package com.wvtone;

import com.wvtone.jobs.GoodBayJob;
import com.wvtone.jobs.HelloWorldJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 * Created by kevin on 2016/8/4.
 */
public class JobFactory {

    public static void main(String[] args) {

        try {
            //1.从StdSchedulerFactory工厂中获取一个任务调度器
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

            //2. 启动调度器
            scheduler.start();
            System.out.println("scheduler is start...");
            //3. 添加定时任务
            //  3.1 定义HelloWorldJob/定义GoodBayJob
            JobDetail helloWorldJob = newJob(HelloWorldJob.class)
                    .withIdentity("HelloWorldJob", "group1")
                    .build();
            JobDetail goodBayJob = newJob(GoodBayJob.class)
                    .withIdentity("GoodBayJob", "group1")
                    .build();

            //  3.2 定义helloworldTrigger，使得job现在就运行，并每隔3s中运行一次，重复运行5次, withRepeatCount(number)设定job运行次数为number+1
            Trigger helloworldTrigger = newTrigger()
                    .withIdentity("HelloWorldTrigger", "group1")
                    .startNow()
                    .withSchedule(simpleSchedule()
                            .withIntervalInSeconds(3)
                            .withRepeatCount(4))
                    .build();
            //  3.2 定义goodBayTrigger，使得job现在就运行，并每隔5s中运行一次，重复运行
            Trigger goodBayTrigger = newTrigger()
                    .withIdentity("GoodBayTrigger", "group1")
                    .startNow()
                    .withSchedule(simpleSchedule()
                            .withIntervalInSeconds(5)
                            .repeatForever())
                    .build();

            //  3.3 交给scheduler去调度
            scheduler.scheduleJob(helloWorldJob, helloworldTrigger);
            scheduler.scheduleJob(goodBayJob, goodBayTrigger);
            //4. 关闭调度器
            //scheduler.shutdown();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }

    }
}
